package reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reggie.common.CustomException;
import reggie.dto.SetmealDto;
import reggie.entity.*;
import reggie.mapper.SetmealMapper;
import reggie.service.CategoryService;
import reggie.service.DishService;
import reggie.service.SetmealDishService;
import reggie.service.SetmealService;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;


    @Transactional
    public void saveWithSetmeal(SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        //保存套餐的基本信息，操作setmeal
        this.save(setmealDto);

        List<SetmealDish> setmealDishes=setmealDto.getSetmealDishes();
        setmealDishes.forEach(iterm->{
           iterm.setSetmealId(setmealDto.getId());
        });
        //保存套餐和菜品的关联信息，操作setmealDish
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    public SetmealDto getWithSetmeal(Long id) {
        //先查询套餐基本信息
        Setmeal setmeal=this.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //再查询口味
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> flavors=setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(flavors);
        return setmealDto;
    }

    @Override
    public void updateWithSetmeal( SetmealDto setmealDto) {
        //根据categoryId查询
        Category category=categoryService.getById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(category.getName());
        //更新setmeal基本表
        this.updateById(setmealDto);

        //更新套餐口味表
        //先删除口味表
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //再插入口味表
        List<SetmealDish> flavors=setmealDto.getSetmealDishes();
        flavors.forEach((iterm)->{
            //通过categoryId查找dish,获取dishId
            iterm.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(flavors);

    }

    @Override
    @Transactional
    public void deleteWithSetmeal(List<Long> ids) {
        //查询套餐状态是否能删除
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count=this.count(queryWrapper);
        if(count>0){
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //可以删除，先删除套餐表中的数据--setmeal
        this.removeByIds(ids);

        //删除setmealDish
        LambdaQueryWrapper<SetmealDish>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }
}
